<!--  -->
<template>
  <div class="meedu-main-body">
    <div class="float-left j-b-flex mb-30">
      <div class="d-flex">
        <el-button @click="$router.push({ name: 'VodCreate' })" type="primary">
          新建课程
        </el-button>
        <el-button
          @click="$router.push({ name: 'CourseCategories' })"
          type="primary"
        >
          录播课分类
        </el-button>
        <el-button
          @click="$router.push({ name: 'CourseComments' })"
          type="primary"
        >
          课程评论
        </el-button>
        <el-button
          @click="$router.push({ name: 'VideoComments' })"
          type="primary"
        >
          课时评论
        </el-button>
        <el-button @click="$router.push({ name: 'VodImport' })" type="primary">
          课时批量导入
        </el-button>
        <OptionBar value="播放器配置" text="录播课设置"></OptionBar>
      </div>
      <div class="d-flex">
        <div>
          <el-input
            class="w-150px"
            v-model="filter.keywords"
            placeholder="课程名称关键字"
          ></el-input>
        </div>
        <div class="ml-10">
          <el-button @click="paginationReset">清空</el-button>
          <el-button @click="filterAct" type="primary">筛选</el-button>
        </div>
        <div class="drawerMore d-flex ml-10" @click="drawer = true">
          <template v-if="showStatus">
            <img src="@/assets/img/icon-filter-h.png" />
            <span class="act">已选</span>
          </template>
          <template v-else>
            <img src="@/assets/img/icon-filter.png" />
            <span>更多</span>
          </template>
        </div>
      </div>
    </div>
    <div class="float-left">
      <el-table
        :header-cell-style="{ background: '#f1f2f9', color: '#333333' }"
        :data="courses"
        style="width: 100%"
        :default-sort="{ prop: 'id', order: 'descending' }"
        @sort-change="sortChange"
      >
        <el-table-column prop="id" sortable label="ID" min-width="6%">
        </el-table-column>
        <el-table-column label="课程" min-width="30%">
          <template slot-scope="scope">
            <!-- <p>123</p> -->
            <div class="d-flex">
              <el-image
                style="width: 120px; height: 90px"
                :src="scope.row.thumb"
                :fit="'cover'"
              ></el-image>
              <p class="ml-10">{{ scope.row.title }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="分类" min-width="12%">
          <template slot-scope="scope">
            <span v-if="scope.row.category">
              {{ scope.row.category.name }}
            </span>
            <span v-else class="c-red">数据不完整</span>
          </template>
        </el-table-column>
        <el-table-column property="charge" label="价格" sortable min-width="8%">
          <template slot-scope="scope"> {{ scope.row.charge }}元 </template>
        </el-table-column>
        <el-table-column
          label="销量"
          property="user_count"
          sortable
          min-width="8%"
        >
          <template slot-scope="scope">
            {{ scope.row.user_count }}
          </template>
        </el-table-column>
        <el-table-column label="上架时间" sortable min-width="14%">
          <template slot-scope="scope">{{
            scope.row.published_at | dateFormat
          }}</template>
        </el-table-column>
        <el-table-column label="是否显示" min-width="8%">
          <template slot-scope="scope">
            <span class="c-green" v-if="scope.row.is_show === 1">· 显示</span>
            <span class="c-red" v-else>· 隐藏</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          min-width="14%"
          align="right"
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="
                $router.push({
                  name: 'CourseVideos',
                  query: { course_id: scope.row.id },
                })
              "
              >课时</el-link
            >
            <el-link
              type="primary"
              class="ml-5"
              @click="
                $router.push({
                  name: 'VodView',
                  params: { id: scope.row.id },
                })
              "
              >学员</el-link
            >
            <el-dropdown class="ml-5">
              <span class="el-dropdown-link">
                <el-link type="primary"
                  >更多
                  <i class="el-icon-arrow-down el-icon--right ml-0"></i>
                </el-link>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <el-link
                    type="primary"
                    :underline="false"
                    @click="
                      $router.push({
                        name: 'VodUpdate',
                        query: { id: scope.row.id },
                      })
                    "
                    >编辑</el-link
                  >
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-link
                    type="primary"
                    :underline="false"
                    @click="
                      $router.push({
                        name: 'CourseAttach',
                        query: { course_id: scope.row.id },
                      })
                    "
                    >附件</el-link
                  >
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-link
                    type="danger"
                    :underline="false"
                    @click="destory(scope.row.id)"
                    >删除</el-link
                  >
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="float-left mt-30 text-center">
      <el-pagination
        @size-change="paginationSizeChange"
        @current-change="paginationPageChange"
        :current-page="pagination.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-drawer :size="360" :visible.sync="drawer" :with-header="false">
      <div class="n-padding-box">
        <div class="tit flex">更多筛选</div>
        <div class="j-flex">
          <el-input
            class="w-300px"
            v-model="filter.keywords"
            placeholder="课程名称关键字"
          ></el-input>
        </div>
        <div class="j-flex mt-20">
          <el-select class="w-300px" v-model="filter.cid" placeholder="分类">
            <el-option
              v-for="(item, index) in filterData.categories"
              :key="index"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div class="j-flex mt-20">
          <el-input
            class="w-300px"
            v-model="filter.id"
            placeholder="课程ID"
          ></el-input>
        </div>
        <div class="j-b-flex mt-30">
          <el-button @click="paginationReset">清空</el-button>
          <el-button @click="filterAct" type="primary">筛选</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import OptionBar from "@/components/option-bar";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    OptionBar,
  },
  data() {
    return {
      loading: false,
      pagination: {
        page: 1,
        size: 10,
        sort: "id",
        order: "desc",
      },
      filter: {
        id: null,
        keywords: null,
        cid: null,
      },
      total: 0,
      courses: [],
      userRemark: [],
      filterData: {
        categories: [],
      },
      drawer: false,
    };
  },
  //监听属性 类似于data概念
  computed: {
    showStatus() {
      if (this.filter.cid || this.filter.id || this.filter.keywords) {
        return true;
      }
      return false;
    },
  },
  //监控data中的数据变化
  watch: {},
  //方法集合

  methods: {
    formatter(row, column) {
      return row.address;
    },
    getCourse() {
      if (this.loading) return;
      this.loading = true;
      let params = {};
      Object.assign(params, this.filter);
      Object.assign(params, this.pagination);
      this.$request.get(`course`, { params }).then((res) => {
        this.loading = false;
        this.courses = res.data.courses.data;
        this.total = res.data.courses.total;
      });
    },
    paginationReset() {
      this.pagination.page = 1;
      this.filter.keywords = null;
      this.filter.id = null;
      this.filter.cid = null;
      this.getCourse();
      this.drawer = false;
    },
    filterAct() {
      this.pagination.page = 1;
      this.getCourse();
      this.drawer = false;
    },

    paginationSizeChange(size) {
      this.pagination.size = size;
      this.getCourse();
    },
    paginationPageChange(page) {
      this.pagination.page = page;
      this.getCourse();
    },
    destory(item) {
      this.$confirm("确认操作？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.loading) return;
          this.loading = true;
          this.$request
            .delete(`course/${item}`)
            .then(() => {
              this.loading = false;
              this.$message.success(this.$t("common.success"));

              this.getCourse();
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    params() {
      this.$request.get(`course/create`).then((res) => {
        let categories = res.data.categories;
        let box = [];
        for (let i = 0; i < categories.length; i++) {
          if (categories[i].children.length > 0) {
            box.push(categories[i]);
            let children = categories[i].children;
            for (let j = 0; j < children.length; j++) {
              children[j].name = "|----" + children[j].name;
              box.push(children[j]);
            }
          } else {
            box.push(categories[i]);
          }
        }
        this.filterData.categories = box;
      });
    },
    sortChange(column) {
      this.pagination.sort = column.prop;
      this.pagination.order = column.order === "ascending" ? "asc" : "desc";
      this.getCourse();
    },
  },
  beforeRouteLeave(to, from, next) {
    if (!this.loading) next();
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getCourse();
    this.params();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
.meedu-main-body {
  width: 100%;
  height: auto;
  float: left;
  background-color: #fff;
  box-sizing: border-box;
  padding: 30px;
  border-radius: 15px;
  margin-bottom: 90px;
  box-shadow: 0 2px 4px 0 hsla(0, 0%, 40%, 0.05);
  min-width: 1180px;
}
.w-150px {
  width: 150px;
}
.ml-0 {
  margin-left: 0;
}
</style>